package com.vfmall.controller.user;

import com.vfmall.constant.JwtClaimsConstant;
import com.vfmall.dto.UserLoginDTO;
import com.vfmall.entity.User;
import com.vfmall.properties.JwtProperties;
import com.vfmall.result.Result;
import com.vfmall.service.UserService;
import com.vfmall.utils.JwtUtil;
import com.vfmall.vo.UserLoginVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
@Api(tags="C端用户相关接口")
public class UserController {
    @Autowired
    public UserService userService;
    @Autowired
    public JwtProperties jwtProperties;


    @PostMapping("login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        User user = userService.wxLogin(userLoginDTO);
        Map<String,Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                map
        );
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

}
